package org.wevil.util;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

/**
 * 请求工具类
 *
 * @author wanglei
 * @since 2023/9/13
 * */
public class RequestUtil {

    /**
     * 数据转字符串
     *
     * @author wanglei
     * */
    public static String request2Str(HttpServletRequest request) {
        StringBuilder jb = new StringBuilder();
        char[] lineChars = new char[2048 * 2048];
        char[] totalChars = new char[2048 * 2048];
        int readLen = 0;
        int totalLen = 0;
        try {
            BufferedReader reader = request.getReader();
            while ((readLen = reader.read(lineChars)) > 0) {
                for (int i = 0; i < readLen; i++) {
                    totalChars[totalLen + i] = lineChars[i];
                }
                totalLen += readLen;
            }
        } catch (Exception e) { /*report an error*/ }
        for(char c : totalChars){
            if(c == '\u0000'){
                break;
            }
            jb.append(c);
        }
        return jb.toString();
    }
}
